<?php
/**
 * 邮件发送类
 */

class MailGateway {

    /**
     * 实例
     * @var MailGateway
     */
    private static $_instance = null;

    /**
     * 邮件类实例
     * @var type
     */
    private $_mail = null;

    /**
     * 构造函数
     */
    private function __construct() {

        $this->_mail = new Mail();
        $smtp = V::config('mail.smtp');
        foreach ($smtp as $domain => $option) {
            $this->_mail->setSmtp($domain, $option);
        }

        $auth = V::config('mail.auth');
        foreach ($auth as $domain => $authConfig) {
           foreach ($authConfig as $username => $info) {
               $this->_mail->setAuth($domain, $username, $info['password'], $info['email']);
           }
        }
    }

    /**
     * 获取实例
     * @return type
     */
    public static function getInstance() {

        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

    /**
     * 发送邮件
     * @param type $fromMail
     * @param type $fromName
     * @param type $toMail
     * @param type $toName
     * @param type $subject
     * @param type $body
     * @param type $isHtml
     * @param type $attach
     * @return type
     */
    public function send($fromMail, $fromName, $toMail, $toName, $subject, $body, $isHtml = true, $attach = '') {

        return $this->_mail->send($fromMail, $fromName, $toMail, $toName, $subject, $body, $isHtml, $attach);
    }
}